<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<?php 
	if( !isset($_COOKIE["username"]) ){
		echo "<script>window.location.href='login.php'</script>";
	}else{
		setcookie("username", $_COOKIE["username"],time() + 900);
	}
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Review Patient</title>
<link href="css/template.css" rel="stylesheet" type="text/css" />
<script src="js/droplinemenu.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/OperateTool.js"></script>
<script type="text/javascript">
//build menu with DIV ID="myslidemenu" on page:
droplinemenu.buildmenu("nav")
</script>

<link href="css/ui-lightness/jquery-ui-1.9.2.custom.css" rel="stylesheet">
<script src="js/jquery-1.8.3.js"></script>
<script src="js/jquery-ui-1.9.2.custom.js"></script>

<!-- datetime select -->
<link rel="stylesheet" href="css/main.css">
<script type="text/javascript" language="javascript" src="js/prototype-1.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-base-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/prototype-date-extensions.js"></script>
<script type="text/javascript" language="javascript" src="js/behaviour.js"></script>
<script type="text/javascript" language="javascript" src="js/datepicker.js"></script>
<link rel="stylesheet" href="css/datepicker.css">
<script type="text/javascript" language="javascript" src="js/behaviors.js"></script>
<script type="text/javascript" language="javascript" src="js/upfile.js"></script>



<script type="text/javascript" language="javascript" src="js/jquery-ui-timepicker-addon.js"></script>


<script type="text/javascript">


	function visitSubmit(){
		
		clearErrorMessage();

		var appointmentNo = document.getElementById("appointmentNo").value;
		var dateVisit = document.getElementById("dateVisit").value;
		var patientNo = document.getElementById("patientNo").value;
		var clinicNo = document.getElementById("clinic").value;
		var isPrinted = 0;
		if( document.getElementById("isPrinted").checked == true ){
			isPrinted = 1;
		}
		
		var isFirstVisit = 0;
		if( document.getElementById("isFirstVisit").checked == true ){
			isFirstVisit = 1;
		}
		var ICD9Code = document.getElementById("ICD9CodeInput").getAttribute("codeNo");

		var procedureObject = document.getElementById("procedure");
		var procedure = procedureObject.value;
		//if( procedure == "" ){
			//procedureObject.focus();
			//document.getElementById("procedureError").innerHTML = "select proceudre";
			//return false;
		//}
		var signs = document.getElementById("signs").value;
		var symptoms = document.getElementById("symptoms").value;
		var diagnosis = document.getElementById("diagnosis").value;
		var treatment = document.getElementById("treatment").value;
		var comments = document.getElementById("comments").value;
		var answerQuestion = document.getElementById("answerQuestion").value;
		var professionalFee = document.getElementById("professionalFee").value;
		if( professionalFee != "" ){
			var reg = new RegExp("^\\d+(\\.\\d+)?$");
			if( reg.test(professionalFee) == false ){
				document.getElementById("professionalFeeError").innerHTML = "professional fee format error!!!";
				return false;
			}
		}
		
		var paymentMethod = document.getElementById("paymentMethod").value;
		
		var isPayed = 0;
		if( document.getElementById("isPayed").checked == true ){
			isPayed = 1;
		}


	
		if( isChange == false ){
			alert("no change occur!!!");
			return;
		}
		
		var postData = "appointmentNo=" + appointmentNo + "&dateVisit=" + dateVisit + "&patientNo=" + patientNo + "&clinicNo=" + clinicNo 
		+ "&isPrinted="  +isPrinted +"&isFirstVisit=" + isFirstVisit + "&ICD9Code=" + ICD9Code + "&procedure=" + procedure + "&signs=" + signs 
		+ "&symptoms=" + symptoms + "&diagnosis=" + diagnosis + "&comments=" + comments + "&answerQuestion=" + answerQuestion + "&professionalFee=" 
		+ professionalFee + "&paymentMethod=" + paymentMethod + "&isPayed=" + isPayed + "&treatment=" + treatment;
		jQuery.ajax({type:'post', async:false, dataType:'json', data:postData, url:'visit_op.php', success:function(result){
				clearErrorMessage();
				if( result.data.status == "success" ){
					alert(result.data.msg);
					document.getElementById("submitButton").value = "Update";
					document.getElementById("deleteButton").disabled = false;
					document.getElementById("printButton").disabled = false;
					isChange = false;
					
				}else{
					alert(result.data.msg);
				}
				
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}
		});
		 
	}


	function clearErrorMessage(){
		document.getElementById("procedureError").innerHTML = "";
		document.getElementById("professionalFeeError").innerHTML = "";
		
	}
	
	function deleteFun(appointmentNo){
		var confirmResult = confirm("are you sure deleting the info of this review ???");
		if( confirmResult == false ){
			return;
		}
		jQuery.ajax({type:'post', async:false, url:'delete_op.php',dataType:'json', data:'No='+ appointmentNo + "&type=4", success:function(result){
				if( result.data.status == 'success' ){
					alert("delete success!!!!");
					isChange = false;
					previousPage();
				}else{
					document.getElementById("msg").innerHTML = result.data.msg;
				}
			},
			error:function(XMLHttpRequest, textStatus, errorThrown){
				alert(XMLHttpRequest.responseText);
			}
		});
	}


	var isChange = false;

	function changeFunc(){
		isChange = true;
		clearErrorMessage();
	}

	function clearErrorMessage(){
		document.getElementById("procedureError").innerHTML = "";
		document.getElementById("professionalFeeError").innerHTML = "";
		
	}

	
	function previousPage(){
		
		if( isChange == true ){
			jQuery("#leaveDialog").dialog("open");
		}else{
			var lastURL = document.referrer;
			//only having appointmentNo can enter this page  
			if( lastURL.indexOf("patient_report.php") == -1 && lastURL.indexOf("appointment.php") == -1 ){
				SetCookie("appointmentPrevious", "");
			} 
			if( lastURL == "" ){
				window.location.href = "appointment.php?appointment_no=" + document.getElementById("appointmentNo").value;
			}else{
				if( lastURL.indexOf("appointment.php") != -1 || lastURL.indexOf("index.php") != -1 ){
					window.location.href = lastURL;	
				}else{
					window.location.href = "appointment.php?appointment_no=" + document.getElementById("appointmentNo").value;
				}
				
			}
		}
	}
	
	
	function gotoPatientReport(){
		var appointmentNo = document.getElementById("appointmentNo").value;
		var postData = "appointmentNo=" + appointmentNo;
		jQuery.ajax({type:'post', async:false, dataType:'json', data:postData, url:'set_print_status.php'});
		window.location.href = "patient_report_pdf.php?appointmentNo=" + appointmentNo;
	}
	
	jQuery(document).ready(function(){

		//initial the operate ICD9_Code dialog
		jQuery("#ICD9Dialog").dialog({
			autoOpen:false,
			modal:true,
			width:800,
			buttons:[
			         {
				         text:"confirm",
				         click:function(){
					         var codeInfo = "";
					         var codeNo = 0;
					         var ICD9SubSubCode = document.getElementById("ICD9SubSubCode").value;
					         var ICD9SubCode = document.getElementById("ICD9SubCode").value;
					         if( ICD9SubSubCode != 0 ){
					        	 var ICD9SubSubCodeObject = document.getElementById("ICD9SubSubCode");
							      	codeNo = ICD9SubSubCode;
							     	var optionList = ICD9SubSubCodeObject.options;
								  	for( var i = 0; i < optionList.length; i++ ){
								         if( optionList[i].selected == true ){
									         codeInfo = optionList[i].innerHTML;
								         }
							        }
					         }else if( ICD9SubCode != 0 ){
						      	var ICD9SubCodeObject = document.getElementById("ICD9SubCode");
						      	codeNo = ICD9SubCode;
						     	var optionList = ICD9SubCodeObject.options;
							  	for( var i = 0; i < optionList.length; i++ ){
							         if( optionList[i].selected == true ){
								         codeInfo = optionList[i].innerHTML;
							         }
						        }
						         

					         }else{
						         var ICD9CodeObject = document.getElementById("ICD9Code");
						         codeNo = ICD9CodeObject.value;
						         var optionList = ICD9CodeObject.options;
						         for( var i = 0; i < optionList.length; i++ ){
							         if( optionList[i].selected == true ){
								         codeInfo = optionList[i].innerHTML;
							         }
						         }
					         }
	
					         document.getElementById("ICD9CodeInput").value = codeInfo;
					         document.getElementById("ICD9CodeInput").setAttribute("codeNo", codeNo);
					         changeFunc();
					         jQuery("#ICD9Dialog").dialog("close");
	
					         
				         }
			         },
			         {
				         text:"cancel",
				         click:function(){
				        	 jQuery("#ICD9Dialog").dialog("close");
				         }
			         }
					]
		});

		//initial the operate procedure dialog
		jQuery("#procedureDialog").dialog({
			autoOpen:false,
			modal:true,
			width:500,
			buttons:[{
						text:"Add",
						id:"procedureOperateId",
						click:function(){
							var procedureNo = document.getElementById("procedureList").value;
							var procedureDescription = document.getElementById("procedureDescription").value;
							jQuery.ajax({type:'post', async:false, data:"procedureNo=" + procedureNo + "&procedureDescription=" + procedureDescription, dataType:'json', url:'procedure_op.php', success:function(result){
									if( result.data.status == "success" ){
										alert(result.data.msg);
										jQuery("#procedureDialog").dialog("close");

										var procedureInDialog = document.getElementById("procedureList");
										if( procedureNo != 0 ){
											for( var i = 0; i < procedureInDialog.options.length; i++ ){
												if( procedureInDialog.options[i].value == procedureNo ){
													procedureInDialog.options[i].text = procedureDescription;
												}
											}
										}else{
											var optionItem = new Option(procedureDescription, result.data.procedureNo);
											optionItem.selected = true;
											procedureInDialog.options.add(optionItem);
											jQuery("#procedureDeleteId").attr("disabled", false);
											jQuery("#procedureDeleteId").removeClass("ui-state-disabled");
											jQuery("#procedureOperateId").text("Update");
											
										}

										var procedureInMain = document.getElementById("procedure");
										if( procedureNo != 0 ){
											for( var i = 0; i < procedureInMain.options.length; i++ ){
												if( procedureInMain.options[i].value == procedureNo ){
													procedureInMain.options[i].text = procedureDescription;
												}
											}
										}else{
											var optionItem = new Option(procedureDescription, result.data.procedureNo);
											procedureInMain.options.add(optionItem);
										}
									}else{
										alert(result.data.msg);
									}
								},
								error:function(XMLHttpRequest, textStatus, errorThrown){
									alert(XMLHttpRequest.responseText);
								}
							});
						}	
					 },
					 {
						 text:"delete",
						 disabled:true,
						 id:"procedureDeleteId",
						 click:function(){
							 var procedureNo = document.getElementById("procedureList").value;
							 var procedureDescription = document.getElementById("procedureDescription").value;
							 jQuery.ajax({type:'post', async:false, data:"No=" + procedureNo + "&type=5", dataType:'json', url:"delete_op.php", success:function(result){
								 	if( result.data.status == "success" ){
									 	alert("delete " + procedureDescription + "success!!!!");
									 	jQuery("#procedureDialog").dialog("close");
									 	document.getElementById("procedureDescription").value = "";

									 	var procedureInDialog = document.getElementById("procedureList");
										for( var i = 0; i < procedureInDialog.options.length; i++ ){
											if( procedureInDialog.options[i].value == procedureNo ){
												procedureInDialog.options.remove(i);
											}
										}


										var procedureInMain = document.getElementById("procedure");
										for( var i = 0; i < procedureInMain.options.length; i++ ){
											if( procedureInMain.options[i].value == procedureNo ){
												procedureInMain.options.remove(i);
											}
										}
								 	}else{
									 	alert(result.data.msg);
								 	}
							 	},
							 	error:function(XMLHttpRequest, textStatus, errorThrown){
									alert(XMLHttpRequest.responseText);
								}
							 });
						 }
					 },
					 {
						 text:"cancel",
						 click:function(){
							 jQuery("#procedureDialog").dialog("close");
						 }
					 }
					
					]
		});


		jQuery("#leaveDialog").dialog({
			autoOpen:false,
			modal:true,
			width:400,
			buttons:[
			         {
				         text:"Yes",
				         click:function(){
					         
				        	if( visitSubmit() == false ){
				        		jQuery(this).dialog("close");
					        	return;
				        	}
				        	isChange = false;
				        	alert("save success!!!");
				        	previousPage();
				         }
				         
			         },
			         {
				         text:"No",
				         click:function(){
				        	 isChange = false;
				        	 previousPage();
				         }
			         },
			         {
				         text:"Cancel",
				         click:function(){
					         jQuery(this).dialog("close");
				         }
			         }
					]
			
		});

	});
	
	
	
	
	
	function codeChange(){
		var ICD9Code = document.getElementById("ICD9Code").value;
		if( ICD9Code == 0 ){
			document.getElementById("ICD9SubDiv").style.display = "none";
			document.getElementById("ICD9SubSubDiv").style.display = "none";
		}else{
			jQuery.ajax({type:'post', data:"no=" + ICD9Code, url:'icd9_code_op.php', dataType:'json', async:false, success:function(result){
					addCode("ICD9SubCode", result);
					if( result.data.length > 0 ){
						document.getElementById("ICD9SubDiv").style.display = "block";
						document.getElementById("ICD9SubSubDiv").style.display = "none";
					}else{
						document.getElementById("ICD9SubDiv").style.display = "none";
						document.getElementById("ICD9SubSubDiv").style.display = "none";
					}
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					alert(XMLHttpRequest.responseText);
				}
				
			});
			
			
		}
	}
	
	function subCodeChange(){
		var ICD9SubCode = document.getElementById("ICD9SubCode").value;
		if( ICD9SubCode == 0 ){
			document.getElementById("ICD9SubSubDiv").style.display = "none";
		}else{
			jQuery.ajax({type:'post', data:"no=" + ICD9SubCode, url:'icd9_code_op.php', dataType:'json', async:false, success:function(result){
					addCode("ICD9SubSubCode", result);
					if( result.data.length > 0 ){
						document.getElementById("ICD9SubSubDiv").style.display = "block";
					}else{
						document.getElementById("ICD9SubSubDiv").style.display = "none";
					}
				},
				error:function(XMLHttpRequest, textStatus, errorThrown){
					alert(XMLHttpRequest.responseText);
				}
				
			});
			
			
		}
	}
	
	
	function addCode(objectId, result){
		var codeObject = document.getElementById(objectId);
		codeObject.options.length = 0;
	
		var firstOptionItem = new Option("", 0);
		firstOptionItem.selected = true;
		codeObject.options.add(firstOptionItem);
		
		for( var i = 0; i < result.data.length; i++ ){
			var optionItem = new Option(result.data[i].codeNumber + "  " + result.data[i].description, result.data[i].no);
			codeObject.options.add(optionItem);
		}
		
	}

	function ICD9Edit(){
		jQuery("#ICD9Dialog").dialog("open");
	}


	function ProcedureEdit(){
		
		jQuery("#procedureDialog").dialog("open");
	}

	function ProcedureChange(objectId){
		var procedureNo = document.getElementById(objectId).value;
		if( procedureNo == 0 ){
			document.getElementById("procedureDescription").value = "";
			jQuery("#procedureDeleteId").attr("disabled", true);
			jQuery("#procedureDeleteId").addClass("ui-state-disabled");
			jQuery("#procedureOperateId").text("Add");
		}else{
			var procedureNoObject = document.getElementById(objectId);
			var procedureDescription = "";
			for( var i = 0; i < procedureNoObject.options.length; i++ ){
				if( procedureNoObject.options[i].value == procedureNo ){
					procedureDescription = procedureNoObject.options[i].text;
					break;
				}
			}
			document.getElementById("procedureDescription").value = procedureDescription;


			jQuery("#procedureDeleteId").attr("disabled", false);
			jQuery("#procedureDeleteId").removeClass("ui-state-disabled");
			jQuery("#procedureOperateId").text("Update");
		}
	}

	function leaveBefore(e){
		if( isChange == true ){
			return true;
		}
	}


	jQuery(document).ready(function(){
		jQuery("#dateVisit").datetimepicker({
			dateFormat:'dd/mm/yy',
			timeFormat:'hh:mm',
			controlType:'select'
		});
	});


	function printInsurance(){
		document.getElementById("container").style.display = "none";
		document.getElementById("insurancePrint").style.display = "block";
		window.print();
		document.getElementById("container").style.display = "block";
		document.getElementById("insurancePrint").style.display = "none";
	}



	
</script>

<style type="text/css">
	#insuranceTable td, #insuranceTable th{
		background-color:#ffffff;
		border:solid 1px #a0c6e5;
	}
</style>
</head>

<body onbeforeunload="return leaveBefore(event);">
	<div id="container">
		<div id="header"></div>
		<?php include_once 'nav.php';?>
		<?php
			$ini_array = parse_ini_file("config/config.ini");
	
			$conn = @mysqli_connect($ini_array["host"], $ini_array["username"], $ini_array["password"], $ini_array["db_name"]) or die("cann't connect the mysql db");
			mysqli_set_charset($conn, "utf8");
			ini_set('default_charset', "UTF-8");
			date_default_timezone_set($ini_array["default_timezone"]);
			
			$appointmentNo = 0;
			$patientNo = 0;
			$dateVisit = "";
			$isPrinted = false;
			$isFirstVisit = false;
			$ICD9CodeNo = 0;
			$ICD9CodeNumber="";
			$ICD9CodeDescription="";
			$procedureNo = 0;
			$clinicNo = 0;
			$symptoms = "";
			$signs = "";
			$diagnosis = "";
			$treatment = "";
			$comments = "";
			$answerQuestion = "";
			$professionalFee = 0;
			$paymentMethod = "VHI";
			$isPayed = false;
			$billPriority = 0;
			
			
			$buttonText = "";
			
			
			$patientFirstName = "";
			$patientSurname = "";
			$patientDOB = "";
			$patientAddress ="";
			$clinicName = "";
			$fee = "";
			$holderName = "";
			$insurerNo = "";
			$insurer = "";
			
			
			
			if( isset($_GET["appointmentNo"]) && preg_match("/^\d+$/", $_GET["appointmentNo"] ) == true ){
				$appointmentNo = mysqli_real_escape_string($conn,$_GET["appointmentNo"]);
				$getVisitInfoSql = "SELECT visit.appointment_no, visit.patient_no, visit.datetime_patient_seen, visit.clinic_no, visit.is_printed, visit.signs, visit.symptoms, visit.diagnosis, visit.treatment, visit.comments, visit.`answer_to_questionaire`, visit.professional_fee, visit.retention_tax, visit.ispay, visit.payment_method, visit.procedure_no, visit.bill_priority, visit.icd9code_no,  appointment.`isFirstVisit`  FROM visit, appointment WHERE appointment.`appointment_no`=visit.`Appointment_no` AND  visit.Appointment_no=".$appointmentNo;
				$result = mysqli_query($conn, $getVisitInfoSql);
				//echo $getVisitInfoSql;
				if( $row = mysqli_fetch_array($result)){
					$patientNo = $row["patient_no"];
					$dateVisit = $row["datetime_patient_seen"];
					$clinicNo = $row["clinic_no"];
					$isPrinted = $row["is_printed"];
						
					$isFirstVisit = $row["isFirstVisit"];
					$ICD9CodeNo = $row["icd9code_no"];
					
					
					$procedureNo = $row["procedure_no"];
					$symptoms = $row["symptoms"];
					$signs = $row["signs"];
					$diagnosis = $row["diagnosis"];
					$treatment = $row["treatment"];
					$comments = $row["comments"];
					$answerQuestion = $row["answer_to_questionaire"];
					$professionalFee = $row["professional_fee"];
					$paymentMethod = $row["payment_method"];
					$isPayed = $row["ispay"];
					$buttonText = "Update";
					//echo $getVisitInfoSql;
					
					if( strcmp($ICD9CodeNo, "" ) != 0 ){
						$getSpecifiedICD9Sql = "select code_number, parent_number, description from icd9_code where no=".$ICD9CodeNo;
						$specifiedICD9Result = mysqli_query($conn, $getSpecifiedICD9Sql);
						if( $row = mysqli_fetch_array($specifiedICD9Result)){
							$ICD9CodeNumber = $row["code_number"];
							$ICD9CodeDescription = $row["description"];
						}
					}
					
					
					$getInsuranceInfoSql = "SELECT  First_name, Surname, Date_birth, address, Clinic_name, Professional_fee, holder_name, insurer_no, insurer FROM visit LEFT OUTER JOIN patient ON patient.`Patient_no` = visit.`Patient_no` LEFT OUTER JOIN clinic ON clinic.`Clinic_no`=visit.`Clinic_no` WHERE visit.`Appointment_no`=$appointmentNo";
					$insuranceResult = mysqli_query($conn, $getInsuranceInfoSql);
					if( $row = mysqli_fetch_array($insuranceResult)){
						$patientFirstName = $row["First_name"];
						$patientSurname = $row["Surname"];
						$patientDOB = date("d-m-Y", strtotime($row["Date_birth"]));
						$patientAddress = $row["address"];
						$fee = $row["Professional_fee"];
						$holderName = $row["holder_name"];
						$insurerNo = $row["insurer_no"];
						$insurer = $row["insurer"];
					}
				}else{
					$getPartVisitInfoSql = "select Patient_no, appointment_datetime, isFirstVisit from appointment where appointment_no=".$appointmentNo;
					$result = mysqli_query($conn, $getPartVisitInfoSql);
					if( $row = mysqli_fetch_array($result) ){
						$patientNo = $row["Patient_no"];
						$dateVisit = $row["appointment_datetime"];
						$isFirstVisit = $row["isFirstVisit"];
						$buttonText = "Add";
					}else{
						echo "<script>alert('appointment no error!!!');window.location.href='index.php';</script>";
					}
				}
			}else{
				echo "<script>alert('no appointment find!!!!');window.location.href='index.php';</script>";
			}
			
			
			
			//translate the bill priority to string
			$billPriorityStr = "";
			if( $billPriority == 0 ){
				$billPriorityStr = "not overdue";
			}else if( $billPriority == 1 ){
				$billPriorityStr = "low";
			}else if( $billPriorityStr == 2 ){
				$billPriorityStr = "middle";
			}else if( $billPriority == 3 ){
				$billPriorityStr = "high";
			}else{
				$billPriorityStr = "unknown";
			}
			
			$getProcedureSql = "select Procedure_no, Procedure_description from procedureList";
			$procedureResult = mysqli_query($conn, $getProcedureSql);
			$getClinicSql = "select Clinic_no, Clinic_name from clinic";
			$clinicResult = mysqli_query($conn,$getClinicSql);
			date_default_timezone_set($ini_array["default_timezone"]);

			
		?>
		
		<div id="main_content" style="text-align:center">
			<form action="visit_op.php" method="post" target="visit_hidden_frame">
			
				<input type="hidden" value="<?php echo $appointmentNo;?>" name="appointmentNo" id="appointmentNo"/>
				<div id="basicDiv" style="margin:10px auto; text-align:left;width:800px">
					<table>
						<tr>
							<td style="width:20%;text-align:right"><label>Patient No:</label></td>
							<td style="width:20%;text-align:left"><input type="text" name="patientNo" readonly="readonly" id="patientNo" value="<?php echo $patientNo;?>"  readonly="readonly"/></td>
							<td style="width:20%;text-align:right"><label>Date of Visit:</label></td>
							<td style="width:20%;text-align:left"><input type="text" name="dateVisit" id="dateVisit" value="<?php echo date("d/m/Y H:i",strtotime($dateVisit));?>" readonly="readonly" onchange="changeFunc()"/></td>
							<td style="width:20%;"></td>
							
						</tr>
						<tr>
							<td style="width:20%;text-align:right"><label>Printed:</label></td>
							<td style="width:20%;text-align:left"><input type="checkbox" name="isPrinted" id="isPrinted" <?php if( $isPrinted == 1 )echo "checked=\"checked\"";?> onclick="changeFunc();"/></td>
							<td style="width:20%;text-align:right"><label>First Visit:</label></td>
							<td style="width:20%;text-align:left"><input type="checkbox" name="isFirstVisit" id="isFirstVisit" <?php if( $isFirstVisit == 1 ) echo "checked=\"checked\"";?> onclick="return false;"/></td>
							<td style="width:20%;"></td>
						</tr>
						<tr>
							<td style="width:20%;text-align:right"><label>ICD9 Codes:</label></td>
							<td style="width:20%;text-align:left"><input type="text" name="ICD9CodeInput" id="ICD9CodeInput" value="<?php echo $ICD9CodeNumber." ".$ICD9CodeDescription;?>" codeNo="<?php echo $ICD9CodeNo?>" readonly="readonly" onchange="changeFunc()"/></td>
							<td style="width:20%;text-align:right"><label>Procedure:</label></td>
							<td style="width:20%;text-align:left">
								<select name="procedure" id="procedure" onchange="changeFunc()" >
									<option value=""></option>
								<?php
								while( $row = mysqli_fetch_array($procedureResult) ){
									$procedureNoInDB = $row["Procedure_no"];
									$procedureDescription = $row["Procedure_description"]; 
								?>
								<option value="<?php echo $procedureNoInDB?>" <?php if( $procedureNoInDB == $procedureNo ) echo "selected=\"selected\"";?>><?php echo $procedureDescription;?></option>
								<?php }?>
								</select>
								
							</td>
							<td style="width:20%;"></td>
						</tr>
						<tr>
							<td style="width:20%;"></td>
							<td style="width:20%;text-align:left"><input type="button" id="editICD9" onclick="ICD9Edit()" value="editICD9"/></td>
							<td style="width:20%;text-align:right;"></td>
							<td style="width:20%;text-align:left"><input type="button" value="Operate Procedure" onclick="ProcedureEdit()"/></td>
							<td style="width:20%;"><label id="procedureError"></label></td>
						</tr>
					</table>
				</div>
				
				<div id="medicalDiv" style="margin:10px auto; text-align:left;width:800px;position:relative;left:75px">
					<table>
						<tr>
							<td style="width:80%;text-align:left;"><label>Symptoms:</label></td>
							<td style="width:20%;text-align:left;"><label>Clinic:</label></td>
						</tr>
						<tr>
							<td style="width:80%;text-align:left;"><textarea cols="60" rows="2" name="symptoms" id="symptoms" onchange="changeFunc()" style="resize:none;"><?php echo $symptoms?></textarea></td>
							<td style="width:20%;text-align:left;">
								<select name="clinic" id="clinic" onchange="changeFunc()">
								<?php 
								while( $row = mysqli_fetch_array($clinicResult)){
									$clinicNoInDB = $row["Clinic_no"];
									$clinicName = $row["Clinic_name"];
								?>
								<option value="<?php echo $clinicNoInDB?>" <?php if( $clinicNoInDB == $clinicNo ) echo "selected=\"selected\"";?>> <?php echo $clinicName;?></option>
								<?php }?>
								</select>
							</td>
						</tr>
						<tr>
							<td colspan="2"><label>Signs:</label></td>
						</tr>
						<tr>
							<td colspan="2"><textarea rows="2" cols="80" name="signs" id="signs" onchange="changeFunc()" style="resize:none;"><?php echo $signs?></textarea></td>
						</tr>
						<tr>
							<td colspan="2"><label>Diagnosis:</label></td>
						</tr>
						<tr>
							<td colspan="2"><textarea rows="2" cols="80" name="diagnosis" id="diagnosis" onchange="changeFunc()" style="resize:none;"><?php echo $diagnosis?></textarea></td>
						</tr>
						<tr>
							<td colspan="2"><label>Treatment:</label></td>
						</tr>
						<tr>
							<td colspan="2"><textarea rows="2" cols="80" name="treatment" id="treatment" onchange="changeFunc()" style="resize:none;"><?php echo $treatment?></textarea></td>
						</tr>
						<tr>
							<td colspan="2"><label>Comments:</label></td>
						</tr>
						<tr>
							<td colspan="2"><textarea rows="2" cols="80" name="comments" id="comments" onchange="changeFunc()" style="resize:none;"><?php echo $comments?></textarea></td>
						</tr>
						<tr>
							<td colspan="2"><label>Answer To Questionaire:</label></td>
						</tr>
						<tr>
							<td colspan="2"><textarea rows="2" cols="80" name="answerQuestion" id="answerQuestion" onchange="changeFunc()" style="resize:none;"><?php echo $answerQuestion?></textarea></td>
						</tr>
					</table>
				</div>
				
				
				

				<div id="methodDiv" style="margin:10px auto;width:800px;position:relative;left:40px;">
					<table>
						<tr>
							
							<td style="width:20%;text-align:right"><label>Payment Method:</label></td>
							<td style="width:20%;text-align:left">
								<select name="paymentMethod" id="paymentMethod" onchange="changeFunc()">
									<option value="Unpaid" selected="selected">Unpaid</option>
									<option value="VHI" <?php if( strcasecmp("VHI", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>VHI</option>
									<option value="Aviva" <?php if( strcasecmp("Aviva", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Aviva</option>
									<option value="Cash" <?php if( strcasecmp("Cash", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Cash</option>
									<option value="Cheque" <?php if( strcasecmp("Cheque", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Cheque</option>
									<option value="Quinn" <?php if( strcasecmp("Quinn", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Quinn</option>
									<option value="Bupa" <?php if( strcasecmp("Bupa", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Bupa</option>
									<option value="ESB" <?php if( strcasecmp("ESB", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>ESB</option>
									<option value="Garda" <?php if( strcasecmp("Garda", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Garda</option>
									<option value="No Fee" <?php if( strcasecmp("No Fee", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>No Fee</option>
									<option value="Paid" <?php if( strcasecmp("Paid", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Paid</option>
									<option value="Leah" <?php if( strcasecmp("Leah", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Leah</option>
									<option value="Glo" <?php if( strcasecmp("Glo", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Glo</option>
									<option value="Other" <?php if( strcasecmp("Other", $paymentMethod) == 0 ) echo "selected=\"selected\""?>>Other</option>							
								</select>
							</td>
							<td style="width:20%;text-align:right"><label>Professional Fee:</label></td>
							<td style="width:20%;text-align:left"><input type="text" name="professionalFee" id="professionalFee" value="<?php echo $professionalFee?>" onchange="changeFunc()"/></td>
							<td style="width:20%;text-align:left"><label id="professionalFeeError"></label></td>
						</tr>
						<tr>
							<td style="width:20%;text-align:right"><label>Payed:</label></td>
							<td style="width:20%;text-align:left"><input type="checkbox" name="isPayed" id="isPayed" <?php if( $isPayed == 1 ) echo "checked=\"checked\""?> onchange="changeFunc()"/></td>
							<td style="width:20%;text-align:right"><div style="display:none;"><label>Bill Priority:</label></div><input type="button" value="print insurance" onclick="printInsurance('<?php echo $appointmentNo?>')" /></td>
							<td style="width:20%;text-align:left"><div style="display:none;"><label><?php echo $billPriorityStr?></label></div></td>
							<td style="width:20%;"></td>
						</tr>
					</table>
				</div>
				
				<div id="operateDiv" style="text-align:center">
					<input type="button" id="submitButton" value="<?php echo $buttonText?>" onclick="visitSubmit()"/>
					<input type="button" id="deleteButton" value="Delete" onclick="deleteFun(document.getElementById('appointmentNo').value)" <?php if( strcmp($buttonText, "Add") == 0){echo "disabled=\"disabled\"";}?>/>
					<input type="button" id="returnButton" value="return" onclick="previousPage()"/>
					<input type="button" id="printButton" value="Print Patient Report" onclick="gotoPatientReport()" <?php if( strcmp($buttonText, "Add") == 0){echo "disabled=\"disabled\"";}?>/>
				</div>
				
			</form>
			<iframe name="visit_hidden_frame" id="visit_hidden_frame" style="display:none;"></iframe>
		</div>
		<div id="msgDiv">
			<label id="msg"></label>
		</div>
		<?php include_once 'footer.php';?>
		
		
		<div id="ICD9Dialog" title="edit ICD9 Code" style="display:none">
			<?php 
				$getICD9Sql = "select no, code_number, parent_number, description from ICD9_CODE where parent_number=0";
				$ICD9Result = mysqli_query($conn, $getICD9Sql);
			?>
			<div style="text-align:right;width:150px;display:inline-block;"><label>ICD9 Code:</label></div>
			<select id="ICD9Code" name="ICD9Code" onchange="codeChange()">
				<option value="0" selected="selected"></option>
				<?php 
					$index = 0;
					$selectNo;
					while( $row = mysqli_fetch_array($ICD9Result)){
						if( $index == 0 ){
							$selectNo = $row["no"];
							$index = 1;
						}
						
						$No = $row["no"];
						$codeNumber = $row["code_number"];
						$parentNumber = $row["parent_number"];
						$description = $row["description"];
				?>
					<option value="<?php echo $No?>"><?php echo $codeNumber."  ".$description?></option>
				<?php 
					}
				?>
			</select>
			<div id="ICD9SubDiv" style="display:none">
				<div style="text-align:right;width:150px;display:inline-block;"><label>ICD9 subCode:</label></div>
				<select id="ICD9SubCode" name="ICD9SubCode" onchange="subCodeChange()">
				</select>
			</div>
			<div id="ICD9SubSubDiv" style="display:none">
				<div style="text-align:right;width:150px;display:inline-block;"><label>ICD9 SubSub Code:</label></div>
				<select id="ICD9SubSubCode" name="ICD9SubSubCode">
				</select>
			</div>
		</div>
		
		
		<div id="procedureDialog" title="operate procedure" style="display:none">
			<?php 
				$procedureResult = mysqli_query($conn, $getProcedureSql);
			?>
			<table>
				<tr>
					<td><label>Procedure List:</label></td>
					<td>
						<select id="procedureList" name="procedureList" onclick="ProcedureChange(this.id)">
							<option value="0">Add Procedure</option>
							<?php 
								while( $row = mysqli_fetch_array($procedureResult)){
									$procedureNo = $row["Procedure_no"];
									$procedureDescription = $row["Procedure_description"];
							?>
								<option value="<?php echo $procedureNo?>"><?php echo $procedureDescription?></option>
							<?php }?>
						</select>
					</td>
				</tr>
				<tr>
					<td><label>procedure title:</label></td>
					<td><input type="text" name="procedureDescription" id="procedureDescription" /></td>
				</tr>
				
				
			</table>
		</div>
		
		<div id="leaveDialog" title="leave dialog" style="display:none">
			<label>the review patient data have change, do  you save before leave?</label>
		</div>
	</div>
	
	<div id="insurancePrint" style="display:none;">
		<div style="margin:0px auto;width:700px;min-height:2em;text-align:center;"><h2><?php echo $insurer?></h2></div>
		<table id="insuranceTable" style="border:1;border-color:#a0c6e5;border-collapse:collapse;width:700px;margin:0px auto;">
			<tr>
				<td style="text-align:right;"><label>Patient First Name:</label></td>
				<td><label id="firstNamePrint"><?php echo $patientFirstName?></label></td>
				<td style="text-align:right;"><label>Patient Surname:</label></td>
				<td><label id="surnamePrint"><?php echo $patientSurname?></label></td>
			</tr>
			<tr>
				<td style="text-align:right;"><label>Holder Name:</label></td>
				<td><label id="holderNamePrint"><?php echo $holderName?></label></td>
				<td style="text-align:right;"><label>Insurance No.:</label></td>
				<td><label id="insuranceNoPrint"><?php echo $insurerNo?></label></td>
			</tr>
			<tr>
				<td style="text-align:right;"><label>DOB:</label></td>
				<td><label id="DOBPrint"><?php echo $patientDOB?></label></td>
				<td style="text-align:right;"><label>Address:</label></td>
				<td><label id="addressPrint"><?php echo $patientAddress?></label></td>
			</tr>
			<tr>
				<td style="text-align:right;"><label>Fee:</label></td>
				<td><label id="feePrint"><?php echo $fee?></label></td>
				<td style="text-align:right;"><label>Clinic Name:</label></td>
				<td><label id="clinicNamePrint"><?php echo $clinicName?></label></td>
			</tr>
		</table>
	</div>
</body>
</html>
